package com.lcxw.hxzbcommon.common;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
import java.util.Date;

public class DateUtils {

    public static void main(String[] args) {
        String startTime = "2020-08-20 15:20:06";
        String endTime = "2020-08-05 15:51:20";
        String term = getTerm(startTime);
        System.out.println(term);
    }

    public static String getJoinTime(String startTime, String endTime) {
        //格式日期格式，在此我用的是"2018-01-24 19:49:50"这种格式
        //可以更改为自己使用的格式，例如：yyyy/MM/dd HH:mm:ss 。。。
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        String term = "";
        try {
            Date now = df.parse(endTime);
            Date date = df.parse(startTime);
            long l = now.getTime() - date.getTime();       //获取时间差
            long month;
            long day = l / (24 * 60 * 60 * 1000);

            if (day < 30) {
                term = "不足一月";
            }

            month = day / 30;
            term = month + "月";

        } catch (Exception e) {
            e.printStackTrace();
        }

        return term;
    }

    public static String getTerm(String startTime, String endTime) {
        //格式日期格式，在此我用的是"2018-01-24 19:49:50"这种格式
        //可以更改为自己使用的格式，例如：yyyy/MM/dd HH:mm:ss 。。。
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        String term = "";
        try {
            Date now = df.parse(endTime);
            Date date = df.parse(startTime);
            long l = now.getTime() - date.getTime();       //获取时间差
            long year;
            long month;
            long day = l / (24 * 60 * 60 * 1000);
            long hour = (l / (60 * 60 * 1000) - day * 24);
            if (day >= 365) {
                year = day / 365;
                day = day - 365 * year;
                term = term + year + "年";
            }
            if (day < 365 && day >= 30) {
                month = day / 30;
                day = day - 30 * month;
                term = term + month + "月";
            }

            term = term + day + "天" + hour + "小时";

        } catch (Exception e) {
            e.printStackTrace();
        }

        return term;
    }


    public static String getTerm(String time) {

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        Date d = null;
        try {
            d = format.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        long delta = (new Date().getTime() - d.getTime()) / 1000;
        if (delta <= 0) {
            return d.toLocaleString();
        }
        if (delta / (60 * 60 * 24 * 365) > 0) {
            return delta / (60 * 60 * 24 * 365) + "年前";
        }
        if (delta / (60 * 60 * 24 * 30) > 0) {
            return delta / (60 * 60 * 24 * 30) + "个月前";
        }
        if (delta / (60 * 60 * 24 * 7) > 0) {
            return delta / (60 * 60 * 24 * 7) + "周前";
        }
        if (delta / (60 * 60 * 24) > 0) {
            return delta / (60 * 60 * 24) + "天前";
        }
        if (delta / (60 * 60) > 0) {
            return delta / (60 * 60) + "小时前";
        }
        if (delta / (60) > 0) {
            return delta / (60) + "分钟前";
        }
        return "刚刚";
    }
}
